<?php
namespace app\kaoping\controller;
use app\BaseController;
use think\facade\Db;
use think\facade\View;
class Order extends First{

	public function _initialize(){
		if (!session('?aid')) {
        	$this->redirect('/login.html');
        }
	}

	public function orders(){

		if (input('pid')!='') {
			$info=db('pici')->where('id',input('pid'))->find();//dump($info);
			if ($info['pay_state']!=1) {
					$n0=0;$n1=0;$n2=0;$n3=0;$n4=0;$n5=0;$n6=0;$n7=0;
					$t0=0;$t1=0;$t2=0;$t3=0;$t4=0;$t5=0;$t6=0;$t7=0;
					$list=array();$num=0;$total=0;$pici=array();$pinfo=array();$discount=0;
					$list=db('card')->select();
					if(session('atype')==1){
						$aid=session('aid');
					}else{
						$aid=$info['aid'];
					}
					$where='s.sid='.$aid.' and sc.pid="'.input('pid').'"';
					if ($list) {
						foreach ($list as $k => $v) {
							$cid=' and sc.cid='.$v['id'];
							$count=db('student_card')->alias('sc')->join('student s','sc.sid=s.id','inner')->where($where.$cid)->count();
							$list[$k]['num']=$count;
							$num+=$count;
							if($v['type']=='CCAT'){
								switch($v['money']){
									case 280:
										$n1+=$count;$t1=280;break;
									case 320:
										$n2+=$count;$t2=320;break;
									case 360:
										$n3+=$count;$t3=360;break;
									case 400:
										$n4+=$count;$t4=400;break;
									case 450:
										$n5+=$count;$t5=450;break;
									case 500:
										$n6+=$count;$t6=500;break;
									case 1500:
										$n7+=$count;$t7=1500;break;
								}
							}elseif($v['type']=='CBDF'){
								if($v['money']==1500){
									$n7+=$count;$t7=1500;
								}else{
									$n0+=$count;	$t0+=$count*$v['money'];
								}
							}
						}
					}
					//$money=controller('index')->discount($num,$total);
					$all_people=$n1+$n2+$n3+$n4+$n5+$n6+$n7;
					$p0=controller('index')->cdbf_price($n0,$t0);
					$p1=controller('index')->ccat_price($all_people,$n1,$t1);
					$p2=controller('index')->ccat_price($all_people,$n2,$t2);
					$p3=controller('index')->ccat_price($all_people,$n3,$t3);
					$p4=controller('index')->ccat_price($all_people,$n4,$t4);
					$p5=controller('index')->ccat_price($all_people,$n5,$t5);
					$p6=controller('index')->ccat_price($all_people,$n6,$t6);
					$p7=controller('index')->ccat_price($all_people,$n7,$t7);
					$total=$t0+$n1*$t1+$n2*$t2+$n3*$t3+$n4*$t4+$n5*$t5+$n6*$t6+$n7*$t7;
					$money=$p0+$p1+$p2+$p3+$p4+$p5+$p6+$p7;//dump($money);die;
					if ($money>0) {
						if ($info['ordernum']!='') {
							if ($money!=$info['real_money']) {
								$data['people_num']=$num;
								$data['all_money']=$total;
								$data['real_money']=$money;
								$data['ordernum']='kaoping_'.$aid.'_'.date('YmdHis').rand(1000,9999);
								$data['id']=$info['id'];
								$res=db('pici')->update($data);
								if ($res) {
									if ($money>50000) {
										die('订单金额超出最大支付范围，请联系客服人员进行线下交易!');
									}else{
										$this->redirect('order_pay?ordernum='.$data['ordernum']);
									}
								}else{
									die('生成订单出错，请重试!');
								}
							}else{
								$this->redirect('order_pay?ordernum='.$info['ordernum']);
							}
						}else{
							$data['people_num']=$num;
							$data['all_money']=$total;
							$data['real_money']=$money;
							$data['ordernum']='kaoping_'.$aid.'_'.date('YmdHis').rand(1000,9999);
							$data['id']=$info['id'];
							$res=db('pici')->update($data);
							if ($res) {
								if ($money>50000) {
									die('订单金额超出最大支付范围，请联系客服人员进行线下交易');
								}else{
									$this->redirect('order_pay?ordernum='.$data['ordernum']);
								}
							}else{
								die('生成订单出错，请重试!');
							}
						}
					}
			}else{
				die('改批次已完成支付，请不要重复支付');
			}
		}
	}

	public function notify(){
		$res=db('pici')->where('ordernum',session('ordernum'))->setfield('pay_state',1);
		if ($res!==false) {
			$this->redirect('index/cost');
		}else{
			die('系统出错，请保存支付账单号作为支付依据');
		}
	}

	public function order_pay($ordernum=''){
		if ($ordernum!='') {
			$info=db('pici')->where('ordernum',$ordernum)->find();//exit('111');
			if ($info) {
				session('ordernum',$info['ordernum']);
				$this->redirect('Wxpay/nativepay');
			}else{
				die('未找到相关订单信息');
			}
		}else{
			die('未找到相关订单信息!');
		}
	}
}